import {
    defineStore,
    createPinia
} from 'pinia';
import {
    login,
    getUserInfo
} from '@/api/user';

export const useCounterStore = defineStore('counter', {
    state: () => {
        const data: {
            token: string,
            userInfo: any,
            isLogin: boolean
        } = {
            token: uni.getStorageSync('TOKEN') || '',
            userInfo: uni.getStorageSync('USERINFO') || {},
            isLogin: (uni.getStorageSync('USERINFO') && uni.getStorageSync('USERINFO').uid) ? true : false
        }
        return data
    },
    actions: {
        setToken(token: string) {
            this.token = token
            uni.setStorageSync('TOKEN', token)
            return Promise.resolve(true)
        },
        async getUserInfo() {
            const res: any = await getUserInfo()
            console.log(res);
            if (res.status === 200) {
                uni.setStorageSync('USERINFO', res.data)
                this.userInfo = res.data
                this.isLogin = true
                return Promise.resolve(true)
            }
        },
        login() {
            uni.login({
                success: (res: any) => {
                    const code = res.code
                    uni.showModal({
                        title: '温馨提示',
                        content: '微信授权登录后才能正常使用小程序功能',
                        cancelText: '拒绝',
                        confirmText: '同意',
                        success: (sucessInfo: any) => {
                            // 如果点了拒绝
                            if (sucessInfo.cancel) return
                            //调用微信小程序的获取用户信息的接口
                            uni.getUserProfile({
                                desc: '用于完善用户资料', // 声明获取用户个人信息后的用途
                                lang: 'zh_CN',
                                success: async (info: any) => {
                                    console.log(info);
                                    const formInfo: any = {
                                        code: code,
                                        iv: info.iv,
                                        encryptedData: info
                                            .encryptedData
                                    }
                                    const res: any = await login(formInfo)
                                    console.log(res);
                                    if (res.status == 200) {
                                        await this.setToken(res.data.token)
                                        const res2: any = await this.getUserInfo()
                                        if (res2) {
                                            uni.showToast({
                                                title: '登录成功',
                                                icon: 'none'
                                            })
                                        }
                                    }
                                },
                                fail(e: any) {
                                    uni.showToast({
                                        icon: 'error',
                                        title: '授权失败！'
                                    })
                                    console.log('获取用户信息失败', e)
                                }
                            })
                        },
                        fail() {
                            console.log("拒绝")
                        },
                        complete() {
                        }
                    })
                }
            })
        },
        logout() {
            uni.removeStorageSync('TOKEN')
            uni.removeStorageSync('USERINFO')
            this.token = ''
            this.userInfo = {}
            this.isLogin = false
        }
    },
});

// 此导出的参数可以直接在js文件里调用
export const pinia = useCounterStore(createPinia())